package com.osmap.openstreetmap.ui.fragment;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
import android.widget.ImageView.ScaleType;

import com.osmap.openstreetmap.R;
import com.osmap.openstreetmap.adapter.NewsPager;
import com.osmap.openstreetmap.adapter.WheelPagerAdapter;
import com.osmap.openstreetmap.bean.ShareBeans;
import com.osmap.openstreetmap.json.HomeJson;
import com.osmap.openstreetmap.ui.BaseFragment;
import com.osmap.openstreetmap.ui.activity.ViewRealtimeOSM;
import com.osmap.openstreetmap.utils.ScreenProperties;

public class HomeFragment extends BaseFragment implements OnClickListener ,AdapterView.OnItemClickListener {

	private ViewPager viewPager;
	private RadioGroup groupPoint;
	private ListView gridView;
	private NewsPager mHomeAdapter;

	private HomeJson mJson;
	private int currentPage;
	private final int MESSAGE_WHAT = 0;
	private final long FLING_PAGE_INTERVAL = 3000;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			viewPager.setCurrentItem(currentPage);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initViews(getView());

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private void initViews(View container) {

		groupPoint = (RadioGroup) container.findViewById(R.id.rg_points);

		viewPager = (ViewPager) container.findViewById(R.id.vp_ads);

		gridView = (ListView) container.findViewById(R.id.gv_hot_recommend);
        gridView.setOnItemClickListener(this);
		initAdapter();
		initViewPager();

	}

	private void initAdapter() {
		mJson = new HomeJson();
		List<ShareBeans> list = new ArrayList<ShareBeans>();
		ShareBeans bean = null;
		for (int i = 0; i < 10; i++) {
			bean = new ShareBeans();
			bean.nickname = "hello " + i;
			bean.desc = "hello " + i;
			bean.publishTime = 10L;
			bean.collectionCount = i;
			bean.imageCount = i;
			bean.miles = i;
			list.add(bean);
		}
		mJson.list = list;
		mHomeAdapter = new NewsPager(getActivity(),list);
		gridView.setAdapter(mHomeAdapter);
	}

	public float getTextSize() {
		float size = getResources().getDimension(R.dimen.home_text_size)
				/ ScreenProperties.getScreenDensity() + 0.5f;

		return size;
	}

	@Override
	public void onClick(View view) {
	}

	private void initViewPager() {
		viewPager = (ViewPager) getView().findViewById(R.id.vp_ads);

		initViewPagerBound();

		LinkedList<View> pageViews = new LinkedList<View>();

		addPageViews(pageViews);
		addPointView(pageViews.size() - 2);

		viewPager.setAdapter(new WheelPagerAdapter(getActivity(), pageViews));
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				renderBrief(arg0);
				adjustWheel(arg0);
				adjustPoint(arg0);
				sendMessageForNextPage(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		viewPager.setCurrentItem(1);

		currentPage = 2;
		handler.sendEmptyMessageDelayed(MESSAGE_WHAT, FLING_PAGE_INTERVAL);

	}

	private void initViewPagerBound() {
		ScreenProperties.initScreenProperties(getActivity());
		// 先算出图片长在屏幕中占多少英寸
		float xInch = (ScreenProperties.getScreenWidth() / ScreenProperties
				.getXdpi());
		// Log.e("xInch", xInch + "Inch");
		// 根据图片宽长比例算出高应该占多少英寸
		double yInch = xInch * (15 / 32.0);
		// Log.e("yInch", yInch + "Inch");
		// 再根据Y轴方向上每英寸多少像素算出图片高应该有多少像素
		int viewPagerHeight = (int) (ScreenProperties.getXdpi() * yInch);
		// Log.e("viewPagerWidth", ScreenProperties.getScreenWidth() + "");
		// Log.e("viewPagerHeight", viewPagerHeight + "");
		RelativeLayout viewPagerBound = (RelativeLayout) getView()
				.findViewById(R.id.rlyt_viewpager);
		viewPagerBound.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, viewPagerHeight));
	}

	private void addPageViews(LinkedList<View> pageViews) {
		ImageView image = new ImageView(getActivity());
		image.setImageResource(R.drawable.home_pager);
		pageViews.add(image);
	}

	private ImageView extraImageView(int i) {
		ImageView imageView = new ImageView(getActivity());
		imageView.setScaleType(ScaleType.CENTER_CROP);
		imageView.setImageResource(R.drawable.home_pager);
		return imageView;

	}

	private void addPointView(int count) {
		// if (count > 0) {
		// for (int i = 0; i < count; i++) {
		// RadioButton radBtn = new RadioButton(getActivity());
		// radBtn.setClickable(false);
		// int width = (int) ScreenProperties.getScreenDensity() * 25;
		// RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
		// width, LayoutParams.WRAP_CONTENT);
		// radBtn.setButtonDrawable(R.drawable.page_control_sel);
		// groupPoint.addView(radBtn, params);
		// }
		// }
		//
		// View v = groupPoint.getChildAt(0);
		// RadioButton radioBtn = (RadioButton) v;
		// radioBtn.setChecked(true);
	}

	private void renderBrief(int arg0) {
	}

	private void adjustWheel(int currentPage) {
		// if (currentPage == wheel.size() + 1) {
		// viewPager.setCurrentItem(1, false);
		// }
		//
		// if (currentPage == 0) {
		// viewPager.setCurrentItem(wheel.size(), false);
		// }
	}

	private void adjustPoint(int pageIndex) {
		// if (pageIndex == 0 || pageIndex == wheel.size() + 1) {
		// return;
		// }
		//
		// View child = groupPoint.getChildAt(pageIndex - 1);
		// if (child instanceof RadioButton) {
		// RadioButton radBtn = (RadioButton) child;
		// radBtn.setChecked(true);
		// }
	}

	private void sendMessageForNextPage(int nowPage) {
		if (handler.hasMessages(MESSAGE_WHAT)) {
			handler.removeMessages(MESSAGE_WHAT);
		}

		currentPage = nowPage + 1;
		handler.sendEmptyMessageDelayed(MESSAGE_WHAT, FLING_PAGE_INTERVAL);
	}

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(getActivity(), ViewRealtimeOSM.class);
        getActivity().startActivity(intent);
    }
}
